package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;


public class DoubleInputDialog extends Dialog implements View.OnClickListener,View.OnFocusChangeListener {
    private Button btnCancel, btnConfirm;
    private EditText editText1;
    private TextView tvTips1,tvTips2;
    private EditText editText2;
    private EditText tempEditText;
    private Context context;
    private AppCompatActivity activity;
    private DoubleDialogCallback callback;
    private int id;

    public DoubleInputDialog(@NonNull Context context, DoubleDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }

    public void setEtInputType(int type1,int type2){
        editText1.setInputType(type1);
        editText2.setInputType(type2);
    }

    public DoubleInputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity) context;
        View root = LayoutInflater.from(context).inflate(R.layout.double_input_dialog, null);
        btnCancel = root.findViewById(R.id.btn_cancel);
        btnConfirm = root.findViewById(R.id.btn_confirm);
        tvTips1 = root.findViewById(R.id.tv_tips1);
        tvTips2 = root.findViewById(R.id.tv_tips2);
        editText1 = root.findViewById(R.id.et_value_1);
        editText2 = root.findViewById(R.id.et_value_2);
        tempEditText=editText1;
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
        setListence();
    }

    private void setListence(){
        editText1.setOnFocusChangeListener(this);
        editText2.setOnFocusChangeListener(this);
    }


    public void showDialog(int id) {
        Log.e("ActionHelper","id="+id);
        tempEditText=editText1;
        InputMethodManagerUtil.inputMethodStatus(activity,tempEditText,false);
        this.id=id;
        show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                callback.doubleDialogInputResult(getValue(editText1),getValue(editText2),id);
                clearData();
                dismiss();
                break;
            case R.id.btn_cancel:
                clearData();
                dismiss();
                break;
            default:
        }
    }

    public void setTips(String tips1,String tips2){
        tvTips1.setText(tips1);
        tvTips2.setText(tips2);
    }

    private void clearData(){
        InputMethodManagerUtil.inputMethodStatus(activity,tempEditText,false);
        editText1.setText("");
        editText2.setText("");
        editText1.setHint("");
        editText2.setHint("");
    }

    public void setHintMsg(String ms1,String ms2){
        editText1.setHint(ms1);
        editText2.setHint(ms2);
    }

    private int getValue(EditText et) {
        int temp = 0;
       try {
           String ss = et.getText().toString().trim();
           if (!TextUtils.isEmpty(ss)) {
               temp = Integer.parseInt(ss);
           }
       }catch (Exception e){
           showToast("数据转换异常，请输入有效内容");
       }
        return temp;
    }

    private void  showToast(String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            tempEditText=(EditText) v;
        }
    }

    public interface DoubleDialogCallback {
        void doubleDialogInputResult(int value1,int value2, int id);
    }
}
